﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum PlatformGroupType { Grass,Winter }

public class PlatformSpawner : MonoBehaviour
{
    public Vector3 startSpawnPos;

    private int spawnPlatformCount;//生成平台的数量
    private ManagerVars vars;
    private Vector3 platformSpawnPosition;//下一个平台的生成位置

    private bool isLeftSpawn = false;//是否向左生成

    private Sprite selectPlatformSprite;//选中的平台图
    private PlatformGroupType groupType;//组合平台类型
    private bool spikeSpawnleft=false;//钉子是否存放在左边
    private Vector3 spikeDirPlatformPos;//钉子位置
    private int afterSpawnSpikeCount;//生成钉子平台之后需要在钉子方向生成的平台数量
    private bool isSpawnSpike;//是否生成钉子
    /// <summary>
    /// 里程碑
    /// 掉落时间*系数
    /// </summary>
    public int milestoneCount=10;
    public float fallTime;
    public float minFallTime;//最少掉落时间
    public float multple;//系数


    private void Awake()
    {
        //添加监听
        EventCenter.AddListener(EventDefine.DecidePath,DecidePath);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.DecidePath,DecidePath);
    }

    // Start is called before the first frame update
    void Start()
    {
        platformSpawnPosition = startSpawnPos;//默认生成位置，初始生成位置
        vars = ManagerVars.GetManagerVars();
        RandomPlatformTheme();//随机主题
        for (int i = 0; i < 5; i++)
        {
            spawnPlatformCount = 5;
            DecidePath();
        }
        //生成人物
        GameObject go = Instantiate(vars.characterPre);
        go.transform.position = new Vector3(0,-1.8f,0);
    }
    private void Update()
    {
        if (GameManager.Instance.IsGameStarted &&
               GameManager.Instance.IsGameOver == false)
        //开始，没有结束
        {
             UpdateFallTime();
        }
    }
    /// <summary>
    /// 随机平台主题
    /// </summary>    
    private void RandomPlatformTheme()
    {
        int ran = Random.Range(0,vars.PlatformSpriteList.Count);
        selectPlatformSprite = vars.PlatformSpriteList[ran];//随机的主题
        //判断是使用冬季主题，春季主题
        if (ran==2)
        {
            groupType = PlatformGroupType.Winter;
        }
        else
        {
            groupType = PlatformGroupType.Grass;
        }
    }

    /// <summary>
    /// 确定路径
    /// </summary>
    private void DecidePath()
    {
        if (isSpawnSpike)
        {
            AfterSpawnSpike();//调用两次
            return;
        }
        if (spawnPlatformCount > 0)
        {
            spawnPlatformCount--;
            SpawnPlatform();
        }
        else
        {
            isLeftSpawn = !isLeftSpawn;//这次向右，下次向左
            //平台生成成功
            spawnPlatformCount =Random.Range(1,4);
            SpawnPlatform();
        }
    }
    /// <summary>
    /// 生成平台
    /// </summary>
    private void SpawnPlatform()
    {
        //相反方向障碍物
        int ranObstacleDir = Random.Range(0,2);

        //生成通用平台
        if (spawnPlatformCount>=1)
        {
            SpawnNormalPlatform(ranObstacleDir);
        }
        //生成组合平台
        else if (spawnPlatformCount==0)
        {
            int ran = Random.Range(0,3);
            //生成通用组合平台
            if (ran==0)
            {
                SpawnCommonPlatformGroup(ranObstacleDir);
            }
            //生成主题组合平台
            else if(ran==1)
            {
                switch (groupType)
                {
                    case PlatformGroupType.Grass:
                        SpawnGrassPlatform(ranObstacleDir);
                        break;
                    case PlatformGroupType.Winter:
                        SpawnWinterPlatform(ranObstacleDir);
                        break;
                    default:
                        break;
                }
            }
            //生成钉子组合平台
            else
            {
                int value = -1;//左边
                if (isLeftSpawn)
                {
                    value = 0;//生成右边方向的钉子
                }
                else
                {
                    value = 1;//生成左边的钉子
                }
                
                SpawnSpikePlatformGroup(value);
                isSpawnSpike = true;//生成钉子
                afterSpawnSpikeCount = 4;//4个

                if (spikeSpawnleft)//钉子在左边
                {
                    spikeDirPlatformPos = new Vector3(platformSpawnPosition.x-1.65f,
                        platformSpawnPosition.y+vars.nextXPos,0);
                }
                else
                {
                    spikeDirPlatformPos = new Vector3(platformSpawnPosition.x + 1.65f,
                       platformSpawnPosition.y + vars.nextXPos, 0);
                }
            }
        }
        //随机生成钻石   -- 修改随机率
       // int ranSpawnDiamond = Random.Range(0,10);
        int ranSpawnDiamond = Random.Range(0, 8);
        if (ranSpawnDiamond==6 && GameManager.Instance.PlayerIsMove)
        {//人物当前是移动的，避免第一步就吃到钻石--实例化钻石
            GameObject go = ObjectPool.Instance.GetDiamond();
            go.transform.position = new Vector3(
                platformSpawnPosition.x,
                platformSpawnPosition.y+0.5f,0);
            go.SetActive(true);//显示
        }

        //更新
        if (isLeftSpawn)//向左生成
        {
            platformSpawnPosition = new Vector3(
                platformSpawnPosition.x-vars.nextXPos,
                platformSpawnPosition.y+vars.nextYPos,0);
        }
        else
        {
            platformSpawnPosition = new Vector3(
                platformSpawnPosition.x + vars.nextXPos,
                platformSpawnPosition.y + vars.nextYPos,0);

        }
    }
    /// <summary>
    /// 生成普通平台（单个）
    /// </summary>
    private void SpawnNormalPlatform(int ranObstacleDir) 
    {
        //GameObject go = Instantiate(vars.normalPlatformPre, transform);
        GameObject go = ObjectPool.Instance.GetNormalPlatform();
        go.transform.position = platformSpawnPosition;
        go.GetComponent<PlatformSprite>().Init(selectPlatformSprite,fallTime, ranObstacleDir);
        go.SetActive(true);
    }
    /// <summary>
    /// 生成通用组合平台
    /// </summary>
    private void SpawnCommonPlatformGroup(int ranObstacleDir)
    {
        //int ran = Random.Range(0,vars.commonPlatformGroup.Count);
        //GameObject go = Instantiate(vars.commonPlatformGroup[ran], transform);
        GameObject go = ObjectPool.Instance.GetCommonPlatform();
        go.transform.position = platformSpawnPosition;
        go.GetComponent<PlatformSprite>().Init(selectPlatformSprite,fallTime, ranObstacleDir);
        go.SetActive(true);
    } 
    /// <summary>
      /// 生成春季平台（单个）
      /// </summary>
    private void SpawnGrassPlatform(int ranObstacleDir)
    {
        //int ran = Random.Range(0, vars.grassPlatformGroup.Count);
        //GameObject go = Instantiate(vars.grassPlatformGroup[ran], transform);
        GameObject go = ObjectPool.Instance.GetGrassPlatform();
        go.transform.position = platformSpawnPosition;
        go.GetComponent<PlatformSprite>().Init(selectPlatformSprite, fallTime, ranObstacleDir);
        go.SetActive(true);
    }
    /// <summary>
    /// 生成冬季平台（单个）
    /// </summary>
    private void SpawnWinterPlatform(int ranObstacleDir)
    {
        //int ran = Random.Range(0, vars.winterPlatformGroup.Count);
        //GameObject go = Instantiate(vars.winterPlatformGroup[ran], transform);
        GameObject go = ObjectPool.Instance.GetWinterPlatform();
        go.transform.position = platformSpawnPosition;
        go.GetComponent<PlatformSprite>().Init(selectPlatformSprite, fallTime, ranObstacleDir);
        go.SetActive(true);
    }
    /// <summary>
    /// 生成钉子组合平台
    /// </summary>
    private void SpawnSpikePlatformGroup(int dir)
    {
        GameObject temp=null;
        if (dir==0)
        {
            spikeSpawnleft = false;
            //temp = Instantiate(vars.spikePlatformRight, transform);
            temp = ObjectPool.Instance.GetRightSpikePlatform();
        }
        else
        {
            spikeSpawnleft = true;
            //temp = Instantiate(vars.spikePlatformLeft, transform);
            temp = ObjectPool.Instance.GetLeftSpikePlatform();
        }
        temp.transform.position = platformSpawnPosition;
        temp.GetComponent<PlatformSprite>().Init(selectPlatformSprite, fallTime, dir);
        temp.SetActive(true);
    }

    /// <summary>
    /// 生成钉子平台之后需要生成的平台
    /// 包括钉子方向，也包括原来的方向
    /// </summary>
    private void AfterSpawnSpike() 
    {
        if (afterSpawnSpikeCount>0)
        {
            afterSpawnSpikeCount--;
            
            //调用两次
            for (int i = 0; i < 2; i++)
            {
                //实例化预制体
                // GameObject temp = Instantiate(vars.normalPlatformPre, transform);
                GameObject temp = ObjectPool.Instance.GetNormalPlatform();
                if (i==0)//生成原来方向的平台
                {
                    if (spikeSpawnleft)//如果钉子在左边,原先的路径就是右边
                    {
                        temp.transform.position = platformSpawnPosition;
                        platformSpawnPosition = new Vector3(
                            platformSpawnPosition.x+vars.nextXPos,
                            platformSpawnPosition.y+vars.nextYPos,0);
                    }
                    else
                    {
                        temp.transform.position = platformSpawnPosition;
                        platformSpawnPosition = new Vector3(
                            platformSpawnPosition.x - vars.nextXPos,
                            platformSpawnPosition.y + vars.nextYPos, 0);
                    }
                }
                //生成钉子方向的钉子
                else
                {
                        temp.transform.position = spikeDirPlatformPos;
                    if (spikeSpawnleft)
                    {
                        spikeDirPlatformPos = new Vector3(
                            spikeDirPlatformPos.x - vars.nextXPos,
                            spikeDirPlatformPos.y + vars.nextYPos, 0);
                    }
                    else
                    {
                        spikeDirPlatformPos = new Vector3(
                            spikeDirPlatformPos.x + vars.nextXPos,
                            spikeDirPlatformPos.y + vars.nextYPos, 0);
                    }
                }
                temp.GetComponent<PlatformSprite>().Init(selectPlatformSprite, fallTime, 1);
                temp.SetActive(true);
            }
        }
        else
        {
            isSpawnSpike = false;
            DecidePath();//重新调用确定路径的方法
        }
    }
    /// <summary>
    /// 更新平台掉落时间
    /// </summary>
    private void UpdateFallTime() 
    {
        if (GameManager.Instance.GetGameScore()>milestoneCount)
        //大于里程碑，更新掉落时间
        {
            milestoneCount *= 2;
            fallTime *= multple;
            if (fallTime<minFallTime)
            {
                fallTime = minFallTime;
            }
        }
    }
}
